<?php

namespace app\controller\admin\myworld;

use app\model\api\WorldGood;
use app\model\api\WorldImg;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use laytp\library\Tree;
use think\facade\Db;

/**
 * Class Good
 * @package app\controller\admin\myworld
 */
class Good extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel = 0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['id' => 'desc'];

    public function _initialize()
    {
        $this->model = new WorldGood();
    }


    /**
     * 我的世界所有物品
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function worldImg()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $data = (new WorldImg())->order($order)->cache(true)->select()->toArray();
        return $this->success('数据获取成功', $data);
    }


    //设置排序
    public function setSort()
    {
        $id = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['sort'] = $fieldVal;
        try {
            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }
            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());

        $insData['name'] = $post['name'];//名称
        $insData['category'] = $post['category'];//分类
        $insData['sub_category'] = $post['sub_category'];//二级分类
        $insData['img'] = $post['img'];//图示
        $insData['big_img'] = $post['big_img'];//大图
        $insData['description'] = $post['description'];//描述
        //基本信息
        if ($post['category']=='方块') {
            if (empty($post['tool_name'])){
                return $this->error('工具不能为空');
            }
            if (empty($post['use_title']) || empty($post['use_content'])){
                return $this->error('用途标题或描述不能为空');
            }
            if (empty($post['get_way_title']) || empty($post['get_way_content1'])){
                return $this->error('获取方式不能为空');
            }
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            $insData['base_attribute'] = json_encode(array(
                'light'=>$post['type1_light'],
                'blast'=>$post['type1_blast'],
                'revive'=>$post['type1_revive'],
                'stack'=>$post['type1_stack'],
                'hardness'=>$post['type1_hardness'],
                'is_deal'=>$post['is_deal']
            ),JSON_UNESCAPED_UNICODE);
        }else if ($post['category']=='食物'){
            if (empty($post['use_title']) || empty($post['use_content'])){
                return $this->error('用途标题或描述不能为空');
            }
            if (empty($post['get_way_title']) || empty($post['get_way_content1'])){
                return $this->error('获取方式不能为空');
            }
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            $insData['base_attribute'] = json_encode(array(
                'back'=>$post['type2_back'],
                'revive'=>$post['type2_revive'],
                'stack'=>$post['type2_stack']
            ),JSON_UNESCAPED_UNICODE);
        }else if ($post['category']=='工具'){
            if (empty($post['get_way_title']) || empty($post['get_way_content1'])){
                return $this->error('获取方式不能为空');
            }
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            $insData['base_attribute'] = json_encode(array(
                'durable'=>$post['type3_durable'],
                'revive'=>$post['type3_revive'],
                'stack'=>$post['type3_stack']
            ),JSON_UNESCAPED_UNICODE);
        }else if($post['category']=='魔咒'){
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            if (empty($post['effect'])){
                return $this->error('效果不能为空');
            }
            $insData['base_attribute'] = json_encode(array(
                'maxLevel'=>$post['type4_maxLevel'],
                'id'=>$post['type4_id'],
                'weight'=>$post['type4_weight']
            ),JSON_UNESCAPED_UNICODE);
        }else if($post['category']=='状态'){
            if (empty($post['get_way_content2'])){
                return $this->error('获取方式不能为空');
            }
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            if (empty($post['effect'])){
                return $this->error('效果不能为空');
            }
            $insData['base_attribute'] = json_encode(array(
                'category'=>$post['type5_category'],
                'type'=>$post['type5_type'],
                'point'=>$post['type5_point'],
                'level'=>$post['type5_level']
            ),JSON_UNESCAPED_UNICODE);
        }else{
            if (empty($post['get_way_title']) || empty($post['get_way_content1'])){
                return $this->error('获取方式不能为空');
            }

            $insData['base_attribute'] = json_encode(array(
                'life'=>$post['type6_life'],
                'defense'=>$post['type6_defense'],
                'experience'=>$post['type6_experience']
            ),JSON_UNESCAPED_UNICODE);
        }
        //查询所有物品图片
        $worldImg = (new WorldImg())->column('img_url','name');

        //工具
        $insData['tool']=json_encode(array(
            'name'=>$post['tool_name'],
            'img'=>!empty($worldImg[$post['tool_name']])?$worldImg[$post['tool_name']]:''
    ),JSON_UNESCAPED_UNICODE);
        //用途

        $use=[];
        $post['use_title'] = explode(',',$post['use_title']);
        $post['use_content'] = explode(',',$post['use_content']);
        foreach ($post['use_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $use[] =array(
                'title'=>$k,
                'content'=>$post['use_content'][$i]
            );
        }
        $insData['use']=json_encode($use,JSON_UNESCAPED_UNICODE);
        //获取方式
        if ($post['category']=='状态'){

            $insData['get_way'] = json_encode(array_filter(explode(',',$post['get_way_content2'])),JSON_UNESCAPED_UNICODE);
        }else{

            $get_way=[];
            $post['get_way_title'] = explode(',',$post['get_way_title']);
            $post['get_way_content1'] = explode(',',$post['get_way_content1']);
            foreach ($post['get_way_title'] as $i=>$k){
                if (empty($k)){
                    continue;
                }
                $get_way[] =array(
                    'title'=>$k,
                    'content'=>$post['get_way_content1'][$i]
                );
            }
            $insData['get_way']=json_encode($get_way,JSON_UNESCAPED_UNICODE);
        }
        //关联物品
        $make_goods_name = [];
        $makeGoods = explode(',',$post['make_goods']);
        foreach($makeGoods as $item){
            if(empty($item)){
                continue;
            }
            $make_goods_name[] = array(
                'name' => $item,
            );
        }
        $make_goods=[];
        foreach ($make_goods_name as $t){
            if (empty($t)){
                continue;
            }
            $make_goods[]=array(
                'name'=>$t['name'],
                'img'=>!empty($worldImg[$t['name']])?$worldImg[$t['name']]:''
        );
        }
        $insData['make_goods']=json_encode($make_goods,JSON_UNESCAPED_UNICODE);

        //相容物品
        $fuse_goods_name = [];
        $fuseGoods = explode(',',$post['fuse_goods']);
        foreach($fuseGoods as $item){
            if(empty($item)){
                continue;
            }
            $fuse_goods_name[] = array(
                'name' => $item,
            );
        }
        $fuse_goods=[];
        foreach ($fuse_goods_name as $t){
            if (empty($t)){
                continue;
            }
            $fuse_goods[]=array(
                'name'=>$t['name'],
                'img'=>!empty($worldImg[$t['name']])?$worldImg[$t['name']]:''
            );
        }
        $insData['fuse_goods']=json_encode($fuse_goods,JSON_UNESCAPED_UNICODE);


        //合成方式

        $position=array(
            '1'=>array('name'=>!empty($post['formula_position1'])?$post['formula_position1']:'','img'=>!empty($worldImg[$post['formula_position1']])?$worldImg[$post['formula_position1']]:''),
            '2'=>array('name'=>!empty($post['formula_position2'])?$post['formula_position2']:'','img'=>!empty($worldImg[$post['formula_position2']])?$worldImg[$post['formula_position2']]:''),
            '3'=>array('name'=>!empty($post['formula_position3'])?$post['formula_position3']:'','img'=>!empty($worldImg[$post['formula_position3']])?$worldImg[$post['formula_position3']]:''),
            '4'=>array('name'=>!empty($post['formula_position4'])?$post['formula_position4']:'','img'=>!empty($worldImg[$post['formula_position4']])?$worldImg[$post['formula_position4']]:''),
            '5'=>array('name'=>!empty($post['formula_position5'])?$post['formula_position5']:'','img'=>!empty($worldImg[$post['formula_position5']])?$worldImg[$post['formula_position5']]:''),
            '6'=>array('name'=>!empty($post['formula_position6'])?$post['formula_position6']:'','img'=>!empty($worldImg[$post['formula_position6']])?$worldImg[$post['formula_position6']]:''),
            '7'=>array('name'=>!empty($post['formula_position7'])?$post['formula_position7']:'','img'=>!empty($worldImg[$post['formula_position7']])?$worldImg[$post['formula_position7']]:''),
            '8'=>array('name'=>!empty($post['formula_position8'])?$post['formula_position8']:'','img'=>!empty($worldImg[$post['formula_position8']])?$worldImg[$post['formula_position8']]:''),
            '9'=>array('name'=>!empty($post['formula_position9'])?$post['formula_position9']:'','img'=>!empty($worldImg[$post['formula_position9']])?$worldImg[$post['formula_position9']]:''),
        );
        $insData['formula']=json_encode(array(
            'position'=>$position,
            'type'=>!empty($post['formula_type'])?$post['formula_type']:'',
            'num'=>!empty($post['formula_num'])?$post['formula_num']:''
        ),JSON_UNESCAPED_UNICODE);
        //效果
        if ($post['category']=='状态' || $post['category']=='魔咒'){
            $insData['effect'] = $post['effect'];//名称
        }else{
            $arr=[];
            $post['effect_title'] = explode(',',$post['effect_title']);
            $post['effect_content'] = explode(',',$post['effect_content']);
            foreach ($post['effect_title'] as $i=>$k){
                if (empty($k)){
                    continue;
                }
                $arr[] =array(
                    'title'=>$k,
                    'content'=>$post['effect_content'][$i]
                );
            }
            $insData['effect']=json_encode($arr,JSON_UNESCAPED_UNICODE);
        }
        //大小
        $arr=[];
        $post['size_title'] = explode(',',$post['size_title']);
        $post['size_content'] = explode(',',$post['size_content']);
        foreach ($post['size_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $arr[] =array(
                'title'=>$k,
                'content'=>$post['size_title'][$i]
            );
        }
        $insData['size']=json_encode($arr,JSON_UNESCAPED_UNICODE);
        //装备
        $equipment_name = [];
        $equipmentName = explode(',',$post['equipment']);
        foreach($equipmentName as $item){
            if(empty($item)){
                continue;
            }
            $equipment_name[] = array(
                'name' => $item,
            );
        }
        $equipment=[];
        foreach ($equipment_name as $t){
            if (empty($t)){
                continue;
            }
            $equipment[]=array(
                'name'=>$t['name'],
                'img'=>!empty($worldImg[$t['name']])?$worldImg[$t['name']]:''
            );
        }
        $insData['equipment']=json_encode($equipment,JSON_UNESCAPED_UNICODE);
        //行为
        $arr=[];
        $post['behavior_title'] = explode(',',$post['behavior_title']);
        $post['behavior_content'] = explode(',',$post['behavior_content']);
        foreach ($post['behavior_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $arr[] =array(
                'title'=>$k,
                'content'=>$post['behavior_content'][$i]
            );
        }
        $insData['behavior']=json_encode($arr,JSON_UNESCAPED_UNICODE);
        //掉落
        $insData['drop'] = json_encode(array_filter(explode(',',$post['drop'])),JSON_UNESCAPED_UNICODE);

        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function info(){
        $id = $this->request->param('id');
        $goods = $this->model->where('id',$id)->find()->toArray();

        $attribute = json_decode($goods['base_attribute'],true);
        $goods['type1_light']=!empty($attribute['light'])?$attribute['light']:'';
        $goods['revive']=!empty($attribute['revive'])?$attribute['revive']:'';
        $goods['stack']=!empty($attribute['stack'])?$attribute['stack']:'';
        $goods['is_deal']=!empty($attribute['is_deal'])?$attribute['is_deal']:'';

        $goods['type1_light']=!empty($attribute['light'])?$attribute['light']:'';
        $goods['type1_blast']=!empty($attribute['blast'])?$attribute['blast']:'';
        $goods['type1_hardness']=!empty($attribute['hardness'])?$attribute['hardness']:'';
        $goods['type2_back']=!empty($attribute['back'])?$attribute['back']:'';
        $goods['type3_durable']=!empty($attribute['durable'])?$attribute['durable']:'';
        $goods['type4_maxLevel']=!empty($attribute['maxLevel'])?$attribute['maxLevel']:'';
        $goods['type4_id']=!empty($attribute['id'])?$attribute['id']:'';
        $goods['type4_weight']=!empty($attribute['weight'])?$attribute['weight']:'';
        $goods['type5_category']=!empty($attribute['category'])?$attribute['category']:'';
        $goods['type5_type']=!empty($attribute['type'])?$attribute['type']:'';
        $goods['type5_point']=!empty($attribute['point'])?$attribute['point']:'';
        $goods['type5_level']=!empty($attribute['level'])?$attribute['level']:'';
        $goods['type6_life']=!empty($attribute['life'])?$attribute['life']:'';
        $goods['type6_defense']=!empty($attribute['defense'])?$attribute['defense']:'';
        $goods['type6_experience']=!empty($attribute['experience'])?$attribute['experience']:'';

        $goods['tool_name']=!empty(json_decode($goods['tool'],true)['name'])?json_decode($goods['tool'],true)['name']:'';
        $goods['use']   = !empty(json_decode($goods['use']))?json_decode($goods['use']):'';
        $goods['get_way']=!empty(json_decode($goods['get_way']))?json_decode($goods['get_way']):'';
        $goods['make_goods'] = !empty($goods['make_goods'])?implode(',',array_column(json_decode($goods['make_goods'],true),'name')):'';
        $goods['fuse_goods'] = !empty($goods['fuse_goods'])?implode(',',array_column(json_decode($goods['fuse_goods'],true),'name')):'';
        $goods['formula_position1']=!empty(json_decode($goods['formula'],true)['position'][1]['name'])?json_decode($goods['formula'],true)['position'][1]['name']:'';
        $goods['formula_position2']=!empty(json_decode($goods['formula'],true)['position'][2]['name'])?json_decode($goods['formula'],true)['position'][2]['name']:'';
        $goods['formula_position3']=!empty(json_decode($goods['formula'],true)['position'][3]['name'])?json_decode($goods['formula'],true)['position'][3]['name']:'';
        $goods['formula_position4']=!empty(json_decode($goods['formula'],true)['position'][4]['name'])?json_decode($goods['formula'],true)['position'][4]['name']:'';
        $goods['formula_position5']=!empty(json_decode($goods['formula'],true)['position'][5]['name'])?json_decode($goods['formula'],true)['position'][5]['name']:'';
        $goods['formula_position6']=!empty(json_decode($goods['formula'],true)['position'][6]['name'])?json_decode($goods['formula'],true)['position'][6]['name']:'';
        $goods['formula_position7']=!empty(json_decode($goods['formula'],true)['position'][7]['name'])?json_decode($goods['formula'],true)['position'][7]['name']:'';
        $goods['formula_position8']=!empty(json_decode($goods['formula'],true)['position'][8]['name'])?json_decode($goods['formula'],true)['position'][8]['name']:'';
        $goods['formula_position9']=!empty(json_decode($goods['formula'],true)['position'][9]['name'])?json_decode($goods['formula'],true)['position'][9]['name']:'';
        $goods['formula_type']=!empty(json_decode($goods['formula'],true)['type'])?json_decode($goods['formula'],true)['type']:'';
        $goods['formula_num']=!empty(json_decode($goods['formula'],true)['num'])?json_decode($goods['formula'],true)['num']:'';

        if ($goods['category']=='状态' || $goods['category']=='魔咒'){
            $goods['effect']=$goods['effect'];
        }else{
            $goods['effect']=json_decode($goods['effect']);
        }
        $goods['size']=json_decode($goods['size']);
        $goods['equipment'] = !empty($goods['equipment'])?implode(',',array_column(json_decode($goods['equipment'],true),'name')):'';
        $goods['behavior']=json_decode($goods['behavior']);
        $goods['drop']=json_decode($goods['drop']);
        return $this->success('获取成功', $goods);
    }

    //编辑
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        $row->name = $post['name'];
        $row->category = $post['category'];
        $row->sub_category = $post['sub_category'];
        $row->img = $post['img'];
        $row->big_img = $post['big_img'];
        $row->description = $post['description'];
        //基本信息
        if ($post['category']=='方块') {
            if (empty($post['tool_name'])){
                return $this->error('工具不能为空');
            }
            if (empty($post['use_title']) || empty($post['use_content'])){
                return $this->error('用途标题或描述不能为空');
            }
            if (empty($post['get_way_title']) || empty($post['get_way_content1'])){
                return $this->error('获取方式不能为空');
            }
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
           $row->base_attribute = json_encode(array(
                'light'=>$post['type1_light'],
                'blast'=>$post['type1_blast'],
                'revive'=>$post['revive'],
                'stack'=>$post['stack'],
                'hardness'=>$post['type1_hardness'],
                'is_deal'=>$post['is_deal']
            ),JSON_UNESCAPED_UNICODE);
        }else if ($post['category']=='食物'){
            if (empty($post['use_title']) || empty($post['use_content'])){
                return $this->error('用途标题或描述不能为空');
            }
            if (empty($post['get_way_title']) || empty($post['get_way_content1'])){
                return $this->error('获取方式不能为空');
            }
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            $row->base_attribute = json_encode(array(
                'back'=>$post['type2_back'],
                'revive'=>$post['revive'],
                'stack'=>$post['stack']
            ),JSON_UNESCAPED_UNICODE);
        }else if ($post['category']=='工具'){
            if (empty($post['get_way_title']) || empty($post['get_way_content1'])){
                return $this->error('获取方式不能为空');
            }
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            $row->base_attribute = json_encode(array(
                'durable'=>$post['type3_durable'],
                'revive'=>$post['revive'],
                'stack'=>$post['stack']
            ),JSON_UNESCAPED_UNICODE);
        }else if($post['category']=='魔咒'){
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            if (empty($post['effect'])){
                return $this->error('效果不能为空');
            }
            $row->base_attribute = json_encode(array(
                'maxLevel'=>$post['type4_maxLevel'],
                'id'=>$post['type4_id'],
                'weight'=>$post['type4_weight']
            ),JSON_UNESCAPED_UNICODE);
        }else if($post['category']=='状态'){
            if (empty($post['get_way_content2'])){
                return $this->error('获取方式不能为空');
            }
            if (empty($post['formula_position1']) && empty($post['formula_position2']) &&empty($post['formula_position3']) &&empty($post['formula_position4']) &&empty($post['formula_position5']) &&empty($post['formula_position6']) &&empty($post['formula_position7']) &&empty($post['formula_position8']) &&empty($post['formula_position9'])){
                return $this->error('合成材料至少一个');
            }
            if (empty($post['formula_type'])){
                return $this->error('合成方式不能为空');
            }
            if (empty($post['formula_num'])){
                return $this->error('合成数量不能为空');
            }
            if (empty($post['effect'])){
                return $this->error('效果不能为空');
            }
            $row->base_attribute = json_encode(array(
                'category'=>$post['type5_category'],
                'type'=>$post['type5_type'],
                'point'=>$post['type5_point'],
                'level'=>$post['type5_level']
            ),JSON_UNESCAPED_UNICODE);
        }else{
            if (empty($post['get_way_title']) || empty($post['get_way_content1'])){
                return $this->error('获取方式不能为空');
            }
            $row->base_attribute = json_encode(array(
                'life'=>$post['type6_life'],
                'defense'=>$post['type6_defense'],
                'experience'=>$post['type6_experience']
            ),JSON_UNESCAPED_UNICODE);
        }

        //查询所有物品图片
        $worldImg = (new WorldImg())->column('img_url','name');

        $tool = [];
        if (!empty($post['tool_name'])) {
            $tool = array(
                'name' => $post['tool_name'],
                'img'=>!empty($worldImg[$post['tool_name']])?$worldImg[$post['tool_name']]:''
            );
        }
        $row->tool = json_encode($tool, JSON_UNESCAPED_UNICODE);
        //用途
        $use=[];
        $post['use_title'] = explode(',',!empty($post['use_title'])?$post['use_title']:'');
        $post['use_content'] = explode(',',!empty($post['use_content'])?$post['use_content']:'');
        foreach ($post['use_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $use[] =array(
                'title'=>$k,
                'content'=>$post['use_content'][$i]
            );
        }
        $row->use=json_encode($use,JSON_UNESCAPED_UNICODE);
        //获取方式
        if ($post['category']=='状态'){
            $row->get_way = json_encode(array_filter(explode(',',$post['get_way_content2'])),JSON_UNESCAPED_UNICODE);
        }else{
            $get_way=[];
            $post['get_way_title'] = explode(',',!empty($post['get_way_title'])?$post['get_way_title']:'');
            $post['get_way_content1'] = explode(',',!empty($post['get_way_content1'])?$post['get_way_content1']:'');
            foreach ($post['get_way_title'] as $i=>$k){
                if (empty($k)){
                    continue;
                }
                $get_way[] =array(
                    'title'=>$k,
                    'content'=>$post['get_way_content1'][$i]
                );
            }
            $row->get_way=json_encode($get_way,JSON_UNESCAPED_UNICODE);
        }
        //关联物品
        $make_goods_name = [];
        $makeGoods = explode(',',!empty($post['make_goods'])?$post['make_goods']:'');
        foreach($makeGoods as $item){
            if(empty($item)){
                continue;
            }
            $make_goods_name[] = array(
                'name' => $item,
            );
        }
        $make_goods=[];
        foreach ($make_goods_name as $t){
            if (empty($t)){
                continue;
            }
            $make_goods[]=array(
                'name'=>$t['name'],
                'img'=>!empty($worldImg[$t['name']])?$worldImg[$t['name']]:''
            );
        }
        $row->make_goods=json_encode($make_goods,JSON_UNESCAPED_UNICODE);

        //相容物品
        $fuse_goods_name = [];
        $fuseGoods = explode(',',!empty($post['fuse_goods'])?$post['fuse_goods']:'');
        foreach($fuseGoods as $item){
            if(empty($item)){
                continue;
            }
            $fuse_goods_name[] = array(
                'name' => $item,
            );
        }
        $fuse_goods=[];
        foreach ($fuse_goods_name as $t){
            if (empty($t)){
                continue;
            }
            $fuse_goods[]=array(
                'name'=>$t['name'],
                'img'=>!empty($worldImg[$t['name']])?$worldImg[$t['name']]:''
            );
        }
        $row->fuse_goods=json_encode($fuse_goods,JSON_UNESCAPED_UNICODE);

        //合成方式
        $position=array(
            '1'=>array('name'=>!empty($post['formula_position1'])?$post['formula_position1']:'','img'=>!empty($post['formula_position1'])?$worldImg[$post['formula_position1']]:''),
            '2'=>array('name'=>!empty($post['formula_position2'])?$post['formula_position2']:'','img'=>!empty($post['formula_position2'])?$worldImg[$post['formula_position2']]:''),
            '3'=>array('name'=>!empty($post['formula_position3'])?$post['formula_position3']:'','img'=>!empty($post['formula_position3'])?$worldImg[$post['formula_position3']]:''),
            '4'=>array('name'=>!empty($post['formula_position4'])?$post['formula_position4']:'','img'=>!empty($post['formula_position4'])?$worldImg[$post['formula_position4']]:''),
            '5'=>array('name'=>!empty($post['formula_position5'])?$post['formula_position5']:'','img'=>!empty($post['formula_position5'])?$worldImg[$post['formula_position5']]:''),
            '6'=>array('name'=>!empty($post['formula_position6'])?$post['formula_position6']:'','img'=>!empty($post['formula_position6'])?$worldImg[$post['formula_position6']]:''),
            '7'=>array('name'=>!empty($post['formula_position7'])?$post['formula_position7']:'','img'=>!empty($post['formula_position7'])?$worldImg[$post['formula_position7']]:''),
            '8'=>array('name'=>!empty($post['formula_position8'])?$post['formula_position8']:'','img'=>!empty($post['formula_position8'])?$worldImg[$post['formula_position8']]:''),
            '9'=>array('name'=>!empty($post['formula_position9'])?$post['formula_position9']:'','img'=>!empty($post['formula_position9'])?$worldImg[$post['formula_position9']]:''),
        );
        $row->formula=json_encode(array(
            'position'=>$position,
            'type'=>$post['formula_type'],
            'num'=>$post['formula_num']
        ),JSON_UNESCAPED_UNICODE);
        //效果
            if ($post['category']=='状态' || $post['category']=='魔咒'){
                $row->effect = !empty($post['effect'])?$post['effect']:'';//名称
            }else{
                $arr=[];
                $post['effect_title'] = explode(',',!empty($post['effect_title'])?$post['effect_title']:'');
                $post['effect_content'] = explode(',',!empty($post['effect_content'])?$post['effect_content']:'');
                foreach ($post['effect_title'] as $i=>$k){
                    if (empty($k)){
                        continue;
                    }
                    $arr[] =array(
                        'title'=>$k,
                        'content'=>$post['effect_content'][$i]
                    );
                }
                $row->effect=json_encode($arr,JSON_UNESCAPED_UNICODE);
            }

        //大小
        $arr=[];
        $post['size_title'] = explode(',',!empty($post['size_title'])?$post['size_title']:'');
        $post['size_content'] = explode(',',!empty($post['size_content'])?$post['size_content']:'');
        foreach ($post['size_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $arr[] =array(
                'title'=>$k,
                'content'=>$post['size_title'][$i]
            );
        }
        $row->size=json_encode($arr,JSON_UNESCAPED_UNICODE);
        //装备
        $equipment_name = [];
        $equipmentName = explode(',',!empty($post['equipment'])?$post['equipment']:'');
        foreach($equipmentName as $item){
            if(empty($item)){
                continue;
            }
            $equipment_name[] = array(
                'name' => $item,
            );
        }
        $equipment=[];
        foreach ($equipment_name as $t){
            if (empty($t)){
                continue;
            }
            $equipment[]=array(
                'name'=>$t['name'],
                'img'=>!empty($worldImg[$t['name']])?$worldImg[$t['name']]:''
            );
        }
        $row->equipment=json_encode($equipment,JSON_UNESCAPED_UNICODE);
        //行为
        $arr=[];
        $post['behavior_title'] = explode(',',!empty($post['behavior_title'])?$post['behavior_title']:'');
        $post['behavior_content'] = explode(',',!empty($post['behavior_content'])?$post['behavior_content']:'');
        foreach ($post['behavior_title'] as $i=>$k){
            if (empty($k)){
                continue;
            }
            $arr[] =array(
                'title'=>$k,
                'content'=>$post['behavior_content'][$i]
            );
        }
        $row->behavior=json_encode($arr,JSON_UNESCAPED_UNICODE);
        //掉落
        $row->drop = json_encode(array_filter(explode(',',!empty($post['drop'])?$post['drop']:'')),JSON_UNESCAPED_UNICODE);

        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }


}